package com.minutemouse.generator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class PowerActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_power);
        
        final ListView listview = (ListView) findViewById(R.id.listView1);
        String[] values = new String[] {"New", "Generic Power","Other1","Other2"};
        
        final ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < values.length; i++) {
        	list.add(values[i]);
        }
        
        final StableArrayAdapter adapter = new StableArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, list);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				if (position == 1)
					viewPower(view);
				else
					if (position == 0)
						createPower(view);
					else
						Toast.makeText(getApplicationContext(), "Clicked ListItem Number " + position, Toast.LENGTH_LONG).show();
				
			}
        	
		});
    }

    private class StableArrayAdapter extends ArrayAdapter<String> {

        HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

        public StableArrayAdapter(Context context, int textViewResourceId,
            List<String> objects) {
          super(context, textViewResourceId, objects);
          for (int i = 0; i < objects.size(); ++i) {
            mIdMap.put(objects.get(i), i);
          }
        }

        @Override
        public long getItemId(int position) {
          String item = getItem(position);
          return mIdMap.get(item);
        }

        @Override
        public boolean hasStableIds() {
          return true;
        }

      }

    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_power, menu);
        return true;
    }

    // Changes to the generation dialog.
    public void createPower(View view) {
    	
    	// Create intent to start new activity.
    	Intent intent = new Intent(this, GeneratorActivty.class);
    	
    	// Since we don't have any arguements to pass, we just
    	// start the new activity.
    	startActivity(intent);
    }
    
    // Switches to view a single power.
    public void viewPower(View view) {
    	
    	// Create intent for new activity.
    	Intent intent = new Intent(this, DetailDisplay.class);
    	
    	/*
    	 * Add arguments etc. 
    	 */
    	
    	//ImageButton imgbtn = (ImageButton) findViewById(R.id.addPowerImageButton);
    	
    	// Start activity.
    	startActivity(intent);
    }
}
